package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"hospital/models"
)

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}
type HospitalController struct {
	beego.Controller
}

func (c *HospitalController) Add() {
	name := c.GetString("name")
	time := c.GetString("time")
	num, _ := c.GetInt("num")
	dates := c.GetString("dates")
	hospital := models.Hospital{
		Name:  name,
		Time:  time,
		Num:   num,
		Dates: dates,
	}
	err := models.DB.Create(&hospital).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: hospital,
		}
		c.ServeJSON()
	}
}
func (c *HospitalController) List() {
	var hospital []models.Hospital
	err := models.DB.Find(&hospital).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功",
			Data: hospital,
		}
		c.ServeJSON()
	}
}
